package main

type ListNode struct {
	Val  int
	Next *ListNode
}

func mergeInBetween(list1 *ListNode, a int, b int, list2 *ListNode) *ListNode {
	hair1 := &ListNode{
		Next: list1,
	}
	n1 := hair1
	n2 := hair1.Next
	i := 0
	var beg, end *ListNode
	for n2 != nil {
		if i == a {
			// 到了插入点
			beg = n1
		}
		if i == b {
			end = n2.Next
		}
		n1 = n2
		n2 = n2.Next
		i++
	}
	beg.Next = list2
	n := list2
	for n.Next != nil {
		n = n.Next
	}
	n.Next = end
	return hair1.Next
}

func makeList(vals ...int) *ListNode {
	head := &ListNode{}
	n := head
	for _, val := range vals {
		n.Next = &ListNode{
			Val: val,
		}
		n = n.Next
	}
	return head.Next
}

func main() {
	list1 := makeList(0, 1, 2, 3, 4, 5)
	list2 := makeList(1000000, 1000001, 1000002)
	ret := mergeInBetween(list1, 3, 4, list2)
	println(ret)
}
